home *** CD-ROM | disk | FTP | other *** search
/ Developer CD Series 1997 January: Mac OS SDK / Dev.CD Jan 97 SDK2.toast / Development Kits (Disc 2) / OpenDoc Development Framework / ODFDev / ODF / Found / ODUtils / ODDebug.h < prev    next >
Encoding:
C/C++ Source or Header  |  1996-09-17  |  5.6 KB  |  181 lines  |  [TEXT/MPS ]

  1. /*
  2.     File:        ODDebug.h
  3.  
  4.     Contains:    Useful debugging macros and functions.
  5.  
  6.     Owned by:    Jens Alfke
  7.  
  8.     Copyright:    © 1993 - 1996 by Apple Computer, Inc., all rights reserved.
  9.  
  10.     Change History (most recent first):
  11.  
  12.          <3>     5/24/96    jpa        1.1MRD: pragma internal eliminates NOPs.
  13.          <2>    .05.1996    NP        1352438: Add IsOptionKeyDown and
  14.                                     IsAnyKeyDown
  15.  
  16.     To Do:
  17. */
  18.  
  19. #ifndef _ODDEBUG_
  20. #define _ODDEBUG_
  21.  
  22. #ifndef _ODTYPES_
  23. #include "ODTypes.h"
  24. #endif
  25.  
  26.  
  27. #ifdef _OD_IMPL_SHARE_UTILS_
  28. #pragma import on
  29. #elif defined(PRAGMA_INTERNAL_SUPPORTED)
  30. #pragma internal on
  31. #endif
  32.  
  33. #ifdef __cplusplus
  34. extern "C" {
  35. #endif
  36.  
  37.  
  38. //==============================================================================
  39. // ODInitExceptions
  40. //==============================================================================
  41.  
  42. void ODInitExceptions( );
  43.  
  44. //==============================================================================
  45. // Misc
  46. //==============================================================================
  47.  
  48. ODBoolean IsThisKeyDown(ODUShort theKey);
  49. inline ODBoolean IsOptionKeyDown() {return IsThisKeyDown(0x3A);}
  50.  
  51. //==============================================================================
  52. // Debug Output
  53. //==============================================================================
  54.  
  55. enum DebugOutputMode {
  56.     kNoOutput,
  57.     kWriteToFile,
  58.     kWriteToDebugWindow,
  59.     kGenerateDebugStrs
  60. };
  61.  
  62. DebugOutputMode    GetOutputMode( );
  63. void            SetOutputMode( DebugOutputMode );
  64.  
  65.  
  66. //==============================================================================
  67. // Warnings
  68. //==============================================================================
  69.  
  70. // WARN has the same syntax as printf but produces a SysBreakStr.
  71. // Warnings are disabled (and generate no code) when ODDebug is off.
  72.  
  73. #define WARN    if(!ODDebug) ; else _Warn
  74.  
  75.  
  76. //==============================================================================
  77. // Assertions
  78. //==============================================================================
  79.  
  80. // These all cause a debugger break if the condition evaluates to false or 0.
  81. // Leading "W" is a Warning: it doesn't raise an exception.
  82. // Trailing "M" means special Message displayed instead of condition.
  83.  
  84. #ifndef __MWERKS__
  85.     #define _FIL_ ""            /* MPW puts entire pathnames in; yuk! */
  86. #else
  87.     #define _FIL_ __FILE__
  88. #endif
  89.  
  90. #define ASSERT( COND, ERR )    \
  91.     if(!ODDebug || (COND)) ; else _AssertionFailed( #COND, _FIL_, ERR )
  92. #define ASSERTM( COND, ERR, MSG )    \
  93.     if(!ODDebug || (COND)) ; else _AssertionFailed( #COND, _FIL_, ERR, MSG )
  94. #define WASSERT( COND )    \
  95.     if(!ODDebug || (COND)) ; else _AssertionFailed( #COND, _FIL_, 0)
  96. #define WASSERTM( COND, MSG )    \
  97.     if(!ODDebug || (COND)) ; else _AssertionFailed( #COND, _FIL_, 0, MSG)
  98.  
  99. // ASSERT_NOT_NULL causes a debugger break and an exception if the parameter
  100. // is NULL. Use this in functions that take a pointer as a parameter but do
  101. // not allow the parameter to be NULL.
  102. // Do **NOT** use this macro to make sure memory allocation succeeded! It
  103. // has no effect in non-debug builds. Use THROW_IF_NULL instead.
  104.  
  105. #define ASSERT_NOT_NULL(PARAM) \
  106.     ASSERT((PARAM)!=kODNULL, kODErrIllegalNullInput)
  107.  
  108.  
  109. //==============================================================================
  110. // Logging
  111. //==============================================================================
  112.  
  113. // PRINT writes to the standard output via somPrintf if ODDebug is on. Use
  114. // SetOutputMode (or the ODDebug menu) to direct output to a file or to the
  115. // DebugWindow app.
  116.  
  117. #define PRINT    if(!ODDebug) ; else somPrintf
  118.  
  119. // LOG is like PRINT but can easily be turned on or off on a per-file basis.
  120. // To enable logging in a source file, you must redefine the symbol LOGGING
  121. // as "1" in that file, otherwise LOG statements will not be compiled. Make
  122. // sure to #undef the symbol before you re-#define it, as some compilers
  123. // won't redefine an already-defined symbol.
  124.  
  125. // PRINT and LOG statements do not generate any code if logging is off.
  126.  
  127. #define LOGGING 0        // Redefine as 1 in source file to enable logging
  128.  
  129. #define LOG        if(!ODDebug || !LOGGING) ; else somPrintf
  130.  
  131.  
  132. //==============================================================================
  133. // Safe Type-Casting
  134. //==============================================================================
  135.  
  136. /*    Use CAST as a safe way to cast a SOM object from one class to another.
  137.     For instance, if "o" is declared as an ODObject*, but your code knows
  138.     it's an ODPart*, you can say:
  139.             ODPart *part = CAST(o,ODPart);
  140.     If ODDebug is turned on, this will do a runtime check and cause an assertion
  141.     failure if o does not point to an ODPart (or subclass). Without ODDebug,
  142.     it degenerates into a simple C-style cast that generates no code.
  143.     
  144.     ASSERT_IS_A is similar to CAST but is used when you just want to assert
  145.     that the pointer points to the right kind of object.
  146. */
  147.  
  148. #if ODDebug
  149.     #define CAST(OBJ, CLASS)    ( (CLASS*)_Cast((OBJ), (somClassDataStructure*) &CLASS##ClassData,        \
  150.                                                         CLASS##_MajorVersion, CLASS##_MinorVersion) )
  151.     #define ASSERT_IS_A(OBJ,CLASS)    ( (void) CAST(OBJ,CLASS) )
  152. #else
  153.     #define CAST(OBJ, CLASS)    ( (CLASS*) (OBJ) )
  154.     #define ASSERT_IS_A(OBJ,CLASS)    /* */
  155. #endif
  156.  
  157.  
  158. //==============================================================================
  159. // Internal goop...
  160. //==============================================================================
  161.  
  162. void _Warn                ( char *fmt, ... );
  163. void _AssertionFailed    ( char *cond,  char *fileName,
  164.                             ODError, char *msg = kODNULL );
  165. #if ODDebug
  166. SOMObject* _Cast( SOMObject *obj, somClassDataStructure *cls, long major, long minor );
  167. #endif
  168.  
  169.  
  170. #ifdef __cplusplus
  171. }
  172. #endif
  173.  
  174. #ifdef _OD_IMPL_SHARE_UTILS_
  175. #pragma import off
  176. #elif defined(PRAGMA_INTERNAL_SUPPORTED)
  177. #pragma internal reset
  178. #endif
  179.  
  180. #endif /*_ODDEBUG_*/
  181.